#!groovy

def call(imageName, imageTag, coverage) {
    echo "基础镜像: ${BASE_IMAGE}, 镜像名: ${imageName}, 镜像tag: ${imageTag}"

    // 单元测试
    stage('单元测试') {
        echo "starting unitTest......"

        sh "mvn -B org.jacoco:jacoco-maven-plugin:prepare-agent clean verify -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true -f pom.xml"
        junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'

        // 单元测试覆盖率要求
        jacoco changeBuildStatus: true, maximumLineCoverage: "${coverage}"
        // 测试报告
        xunit([JUnit(deleteOutputFiles: true, failIfNotNew: true, pattern: '**/target/surefire-reports/*.xml', skipNoTestFiles: false, stopProcessingIfError: true)])
    }

    // sonar分析
    stage('静态代码检查') {
        echo "starting sonar analyze ......"
        withSonarQubeEnv('sonarqube') {
            sh "mvn sonar:sonar"
        }
    }

    stage('Quality Gate') {
        sleep(10)
        timeout(time: 1, unit: 'HOURS') {
            waitForQualityGate abortPipeline: true
        }
    }

    // docker打包
    stage('docker打包') {
        sh "mvn clean package -U -Dmaven.test.skip=true -Dimage_name=${imageName} docker:build"
    }

    dockerPush(imageName, imageTag)
}